//
//  SheetView.m
//  QoilPublic
//
//  Created by SunChao on 17/9/8.
//  Copyright © 2017年 杨凯. All rights reserved.
//



#import "SheetView1.h"
#import "GlobalStarCell.h"
#import "DetailStarCell.h"
#import "MarkCell.h"
#import "EvaluateTextCell.h"
#define ConViewH  250
@interface SheetView1()<UITableViewDelegate,UITableViewDataSource,StarSelectedDelegate,AdviseTextDelegate>
{
    UIView *_contentView;
    UITableView *_tableView;
    NSString *_star1;
    NSString *_star2;
    NSString *_star3;
    NSString *_adviseString;
    NSArray *_textArr;
}

@end

@implementation SheetView1


- (id)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame])
    {
        [self initContent];
    }
    
    return self;
}

- (void)initContent
{
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    //alpha 0.0  白色   alpha 1 ：黑色   alpha 0～1 ：遮罩颜色，逐渐
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)]];
    
    if (_contentView == nil)
    {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - ConViewH, kScreenWidth, ConViewH)];
        _contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:_contentView];
    }
   
    [self createTableView];
    [self gainStarText];
   
}

-(void)createTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, ConViewH) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    [_contentView addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:@"GlobalStarCell" bundle:nil] forCellReuseIdentifier:@"GlobalStarCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"DetailStarCell" bundle:nil] forCellReuseIdentifier:@"DetailStarCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"MarkCell" bundle:nil] forCellReuseIdentifier:@"MarkCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"EvaluateTextCell" bundle:nil] forCellReuseIdentifier:@"EvaluateTextCell"];
   
}

#pragma mark--星级文字
- (void)gainStarText{
    
    NSString *url = [QParamService urlWithAction:@"order/getEvalType"];
    NSDictionary *params = [QParamService GainStarText];
    [QViewUtility showMBHudToView:self hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:params completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            _textArr = [basModel.items objectForKey:@"list"];
            
            [_tableView reloadData];
            
        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}


#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        
        return 3;
    }
   
    if (section == 2) {
        return 1;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        GlobalStarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GlobalStarCell"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;

    }
    if (indexPath.section == 1) {
        NSMutableArray *arr = [NSMutableArray new];
        
        for (NSDictionary *tmpDic in _textArr) {
            NSString *content = [tmpDic objectForKey:@"content"];
            [arr addObject:content];
        }
        DetailStarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailStarCell"];
        if (arr.count>0) {
             NSString *str  = [arr objectAtIndex:indexPath.row];
            NSString *star = @"";
            if (indexPath.row == 0) {
                star = _star1;
            }
            if (indexPath.row == 1) {
                star = _star2;
            }
            if (indexPath.row == 2) {
                star = _star3;
            }
             [cell SetupStarViewWith:str indexPathOfRow:indexPath.row canEdit:NO starNum:star evaluate:_adviseString];
        }
       
        cell.delegate = self;
        cell.tag = indexPath.row + 1;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }

    if (indexPath.section == 2) {
        /*EvaluateTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EvaluateTextCell"];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
         return cell;*/
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DataTableViewCell"];
        
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DataTableViewCell"];
        }
        cell.textLabel.width = 200;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        if (_adviseString == nil ||[_adviseString isEqualToString:@""]) {
            cell.textLabel.text = @"没有评论内容~";
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
        }else{
            cell.textLabel.text = [NSString stringWithFormat:@"评价内容为:%@",_adviseString];
        }
        
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        
        return 50;
    }
    if (indexPath.section == 1) {
        
        return 50;
    }

    if (indexPath.section == 2) {
        
        return 50;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
//展示从底部向上弹出的UIView（包含遮罩）
- (void)showInView:(UIView *)view
{
    if (!view)
    {
        return;
    }
    
    [view addSubview:self];
    
    [view addSubview:_contentView];
    
    [_contentView setFrame:CGRectMake(0, kScreenHeight, kScreenWidth, ConViewH)];
    
    
    NSLog(@"DataSource：%@",_dataSource);
    NSMutableArray *array = [NSMutableArray new];
    
    for (NSDictionary *tmpDic in _dataSource) {
        
        if ([tmpDic.allKeys containsObject:@"name"]) {
            NSString *str = [tmpDic objectForKey:@"name"];
            NSDictionary *dic = @{@"content":str};
            [array addObject:dic];
        }
    }
    
    
    NSDictionary *dic1 = [_dataSource objectAtIndex:0];
    NSString *stars1 = [dic1 objectForKey:@"stars"];
    _star1 = stars1;
    
    NSDictionary *dic2 = [_dataSource objectAtIndex:1];
    NSString *stars2 = [dic2 objectForKey:@"stars"];
    _star2 = stars2;
    
    NSDictionary *dic3 = [_dataSource objectAtIndex:2];
    NSString *stars3 = [dic3 objectForKey:@"stars"];
    _star3 = stars3;
    
    if (_dataSource.count == 4) {
        
        NSDictionary *dic4 = [_dataSource objectAtIndex:3];
        
       NSDictionary *dic5 = [self processDictionaryIsNSNull:dic4];
        
        
        if ([[dic5 objectForKey:@"content" ] isEqualToString:@""]||[dic5 objectForKey:@"content" ]  == nil) {
            _adviseString = @"";
            
        }else{
            NSString *advising = [dic4 objectForKey:@"content"];
            _adviseString = advising;
        }
    }
    
    _textArr = array;
    
    [_tableView reloadData];
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.alpha = 1.0;
        
        [_contentView setFrame:CGRectMake(0, kScreenHeight - ConViewH, kScreenWidth, ConViewH)];
        
    } completion:^(BOOL finished) {
        
    }];
}

- (id) processDictionaryIsNSNull:(id)obj{
    const NSString *blank = @"";
    
    if ([obj isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *dt = [(NSMutableDictionary*)obj mutableCopy];
        for(NSString *key in [dt allKeys]) {
            id object = [dt objectForKey:key];
            if([object isKindOfClass:[NSNull class]]) {
                [dt setObject:blank
                       forKey:key];
            }
            else if ([object isKindOfClass:[NSString class]]){
                NSString *strobj = (NSString*)object;
                if ([strobj isEqualToString:@"<null>"]) {
                    [dt setObject:blank
                           forKey:key];
                }
            }
            else if ([object isKindOfClass:[NSArray class]]){
                NSArray *da = (NSArray*)object;
                da = [self processDictionaryIsNSNull:da];
                [dt setObject:da
                       forKey:key];
            }
            else if ([object isKindOfClass:[NSDictionary class]]){
                NSDictionary *ddc = (NSDictionary*)object;
                ddc = [self processDictionaryIsNSNull:object];
                [dt setObject:ddc forKey:key];
            }
        }
        return [dt copy];
    }
    else if ([obj isKindOfClass:[NSArray class]]){
        NSMutableArray *da = [(NSMutableArray*)obj mutableCopy];
        for (int i=0; i<[da count]; i++) {
            NSDictionary *dc = [obj objectAtIndex:i];
            dc = [self processDictionaryIsNSNull:dc];
            [da replaceObjectAtIndex:i withObject:dc];
        }
        return [da copy];
    }
    else{
        return obj;
    }
}

//移除从上向底部弹下去的UIView（包含遮罩）
- (void)disMissView
{
    [_contentView setFrame:CGRectMake(0, kScreenHeight - ConViewH, kScreenWidth, ConViewH)];
    [UIView animateWithDuration:0.3f animations:^{
                         
        self.alpha = 0.0;
                         
        [_contentView setFrame:CGRectMake(0, kScreenHeight, kScreenWidth, ConViewH)];
                    
        
    }completion:^(BOOL finished){
                         
        [self removeFromSuperview];
        [_contentView removeFromSuperview];
        
    }];
}

- (void)gainStarWithText: (NSString *) text indexPathOfRow:(NSUInteger )row{
    if (row == 0) {
        _star1 = text;
    }
    if (row == 1) {
        _star2 = text;
    }
    if (row == 2) {
        _star3 = text;
    }
}
-(void)gainStarWithText:(NSString *)text{

    _adviseString = text;
}


@end
